# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=bzip2

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.0.6
pkgrel=2
pkgdesc="A high-quality data compression program (mingw-w64)"
arch=('any')
url="http://sources.redhat.com/bzip2"
license=("custom")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
options=('strip' 'staticlibs')
source=("http://www.bzip.org/${pkgver}/bzip2-${pkgver}.tar.gz"
    "bzip2-1.0.5-slash.patch"
    "bzip2-1.0.4-bzip2recover.patch"
    "bzgrep-debian-1.0.5-6.all.patch"
    "bzip2-cygming-1.0.6.src.all.patch"
    "bzip2-buildsystem.all.patch"
    "bzip2-1.0.6-progress.all.patch")
sha1sums=('3f89f861209ce81a6bab1fd1998c0ef311712002'
          '809c3a95f6f6c26463d804b345d62eed8420c6a6'
          '85ce76fdb67f52f152bba368c1de81132dcdbdb5'
          'dc5a01986a3b84ae619bc3899e2c2eaa83a60197'
          'ce4af0cf55888e96fccb16eecd17faa1a3837aec'
          '55f546dfbd4c19d7e51dac58fe8552a6a59906e8'
          '353b74786f3f8fcddb73691780280a72ef77b85d')

prepare() {
  cd "$srcdir/${_realname}-$pkgver"
  patch -p1 -i "$srcdir/"bzip2-1.0.4-bzip2recover.patch
  patch -p1 -i "$srcdir/"bzip2-1.0.5-slash.patch
  patch -p1 -i "$srcdir/"bzgrep-debian-1.0.5-6.all.patch
  patch -p1 -i "$srcdir/"bzip2-cygming-1.0.6.src.all.patch
  patch -p1 -i "$srcdir/"bzip2-buildsystem.all.patch
  patch -p1 -i "$srcdir/"bzip2-1.0.6-progress.all.patch

  autoreconf -fi
}

build() {
  unset LDFLAGS
  mkdir -p "${srcdir}/build-${CARCH}"
  cd "${srcdir}/build-${CARCH}"
  "${srcdir}"/${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-shared
  make all-dll-shared
}

check() {
  cd "${srcdir}/build-${CARCH}"
  make -k check
}

package() {
  cd "${srcdir}/build-${CARCH}"
  make DESTDIR="$pkgdir" install
  #make PREFIX="${pkgdir}${MINGW_PREFIX}" install
  find "${pkgdir}${MINGW_PREFIX}" -name '*.def' -o -name '*.exp' | xargs -rtl1 rm
  #rm "${pkgdir}${MINGW_PREFIX}/bin/bz"{diff,grep,more}
}
